package com.acme.benchmarks;

import com.acme.HashMapFactory;

import java.util.Map;

public class GetTimeBenchmark extends Benchmark {

    String accString = "";

    public GetTimeBenchmark(int iterations, int growFactor, HashMapFactory factory) {
        super(iterations, growFactor, factory);
    }

    @Override
    protected void runIteration(int iterationSize) {

        Map<Integer, Integer> hashMap = generateMap(iterationSize);

        int iterations = 100;

        int size = hashMap.size();

        long totalTime = 0;

        Stopwatch sw = new Stopwatch();
        sw.start();

        for (int i = 0; i < iterations; i++) {
            int position = (int) (size * Math.random());
            hashMap.get(position);
        }

        totalTime += sw.getElapsed();
        accString += ("" + hashMap.size()).substring(0, 1);

        log(iterationSize + ":" + totalTime / iterations);

    }

    private static class Stopwatch {

        private long time;

        public void start() {
            time = System.nanoTime();
        }

        public long getElapsed() {
            return System.nanoTime() - time;
        }

    }

}
